﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UIManage
{
	private UICamera _UICamera;
	private UIAnchor Center;
	private UIAnchor Top;
	private UIAnchor Down;
	private UIAnchor Left;
	private UIAnchor Right;

	public UI_MAIN UIMain;
	public UI_LOGO UILogo;
	public UI_Level UILevel;

	private List<MonoBehaviour> _UIStack;

	public const int UI_NUMBER_LOGO = 0;
	public const int UI_NUMBER_MAIN = 1;
	public const int UI_NUMBER_Level = 2;
	
	public UIManage()
	{
		UIAnchor[] anchors = GameObject.FindObjectsOfType<UIAnchor>();
		foreach ( UIAnchor a in anchors )
		{
			switch( a.name )
			{
			case "Center":
				Center = a;
				break;
			case "Top":
				Top = a;
				break;
			case "Down":
				Down = a;
				break;
			case "Left":
				Left = a;
				break;
			case "Right":
				Right = a;
				break;
			} // switch
		}

		_UIStack = new List<MonoBehaviour>();
	}

	public void ShowUI( int uiNumber )
	{
		string strName = string.Empty;
		if ( uiNumber == UI_NUMBER_MAIN )
			strName = "UI_MAIN" ;
		else if ( uiNumber == UI_NUMBER_LOGO )
			strName = "UI_LOGO" ;
		else if ( uiNumber == UI_NUMBER_Level )
			strName = "UI_Level" ;

		GameObject uiGO = Resources.Load<GameObject>( "UI/"+strName );



		if ( uiNumber == UI_NUMBER_MAIN )
		{
			if ( UIMain == null )
			{
				GameObject ui = NGUITools.AddChild(Center.gameObject,uiGO);
				UIMain = ui.AddComponent<UI_MAIN>();
				_UIStack.Insert(0,UIMain);
				return;
			}
			if ( !_UIStack.Contains(UIMain as MonoBehaviour) )
				_UIStack.Insert(0,UIMain);
			else
			{
				_UIStack.Remove(UIMain );
				_UIStack.Insert(0,UIMain);
				UIMain.gameObject.SetActive(true);
			}
		}
		else if ( uiNumber == UI_NUMBER_LOGO )
		{
			if ( UILogo == null )
			{
				GameObject ui = NGUITools.AddChild(Center.gameObject,uiGO);
				UILogo = ui.AddComponent<UI_LOGO>();
				_UIStack.Insert(0,UILogo);
				return;
			}
			if ( !_UIStack.Contains(UILogo as MonoBehaviour) )
				_UIStack.Insert(0,UILogo);
			else
			{
				_UIStack.Remove(UILogo );
				_UIStack.Insert(0,UILogo);
				UILogo.gameObject.SetActive(true);
			}
		}
		else if ( uiNumber == UI_NUMBER_Level )
		{
			if ( UILevel == null )
			{
				GameObject ui = NGUITools.AddChild(Center.gameObject,uiGO);
				UILevel = ui.AddComponent<UI_Level>();
				_UIStack.Insert(0,UILevel);
				return;
			}
			if ( !_UIStack.Contains(UILevel as MonoBehaviour) )
				_UIStack.Insert(0,UILevel);
			else
			{
				_UIStack.Remove(UILevel );
				_UIStack.Insert(0,UILevel);
				UILevel.gameObject.SetActive(true);
			}
		}

		int i = 0 ;
		foreach ( MonoBehaviour mon in _UIStack )
		{
			mon.transform.localPosition = new Vector3(0,0,i );
			i+=100;
		}
	}

	public void CloseUI( int uiNumber )
	{
		string strName = string.Empty;
		if ( uiNumber == UI_NUMBER_MAIN )
		{
			if ( UIMain == null )
				return;
			UIMain.Pnl_Main.clipRange = new Vector4( 0,
			                                        0,
			                                        UIMain.Pnl_Main.clipRange.z,
			                                        UIMain.Pnl_Main.clipRange.w);
			UIMain.gameObject.SetActive(false);
		}
		else if ( uiNumber == UI_NUMBER_LOGO )
		{
			if ( UILogo == null )
				return;
			UILogo.gameObject.SetActive(false);
		}
		else if ( uiNumber == UI_NUMBER_Level )
		{
			if ( UILevel == null )
				return;
			UILevel.gameObject.SetActive(false);
		}



	}




} // UIManage()
